﻿from rrTile import *
from rrCursor import *

#*******************************************************************************
#
#	MISC FUNCTIONS
#
#*******************************************************************************

def handle_key(player, objects, map):
	c=getch()
	action=NONE
	
	if c==KEY_RIGHT:
		action=player.move(1,0, map, objects)
	elif c==KEY_LEFT:
		action=player.move(-1,0, map, objects)
	elif c==KEY_UP:
		action=player.move(0,-1, map, objects)
	elif c==KEY_DOWN:
		action=player.move(0,1, map, objects)

	elif c==105: #i
		action=player.draw_inventory(objects, map)
	elif c==101: #e
	#draw equiped item
		action=NONE
	elif c==112: #p
		action=player.pick_up(objects, map)
	elif c==111: #o 
	#open door at given direction
		action=player.open_door(objects, map)
	elif c==27 or c==32: #esc or space
		action=NONE
	elif c==113: #q
		action=QUIT
	elif c==59: #;
		curs=Cursor(player)
		action=curs.get_info(player, objects, map)
		
	return action

def is_case_blocked(x,y, map, objects):
	if map[x][y].blocked:
		return True
	for o in objects:
		if o.blocked and o.x==x and o.y==y:
			return True
	
	return False
	
def compute_fov(o, objects, map):
	for i in range(MAP_H):
		for j in range(MAP_W):
			map[j][i].seen=False
	
	for obj in objects:
		obj.visible=False
		
	for i in range(360):
		x=math.cos(i*0.01745)
		y=math.sin(i*0.01745)
		do_fov(o,x,y, objects, map)

def do_fov(o,x,y, objects, map):
	ox=float(o.x)
	oy=float(o.y)
	
	if FOV > FOW:
		k=FOV
	else:
		k=FOW
	
	for i in range(k):
		if i<FOV:
			map[int(ox)][int(oy)].explored=True
		if i<FOW:
			map[int(ox)][int(oy)].seen=True
			for obj in objects:
				if obj.x==int(ox) and obj.y==int(oy):
					obj.visible=True
		if map[int(ox)][int(oy)].block_sight:
			break
		ox+=x
		oy+=y
		
def render_all(objects):
#print every objects on screen
	for o in objects:
		if o.visible:
			o.draw()
			
def check_multiple_item(player, objects):
#if there are more than one item on the current location print the list
	list=[]
	i=0
	l=1
	for o in objects:
		if o.x==player.x and o.y==player.y and o!=player:
			list.append(o.name)
			i+=1
	if get_number_of_items(player,objects)>=1:
		mvaddstr(0,0,'things that are here:')
		for i in list:
			mvaddstr(l,0,i)
			l+=1

def get_number_of_items(player, objects):
	i=0
	for o in objects:
		if o.x==player.x and o.y==player.y and o!=player:
			i+=1
	return i
			
def blit_screen(player, objects, map):
	mvaddstr(0,0,'')
	clrtoeol()
	refresh()
	compute_fov(player, objects, map)
	draw_map(map)
	render_all(objects)


#pauses the game until the player answers with a direction
def wait_direction():
	x = []
	for i in range(258,262):
		x.append(i)
	
	mvaddstr(0,0,"which direction?")
	clrtoeol()
	refresh()
	
	c=0
	while c not in x:
		c=getch()
	
	return c
	
#pause the game until player answers which item in a list he want (list item ranging from 0 to 9
#0=48 1=49 2=50 3=51 4=52 5=53 6=54 7=55 8=56 9=57
def wait_answer(n):
	if t!=0:
		x = []
		for i in range(48,n+48):
			x.append(i)
		
		mvaddstr(1,0,"which one?")
		c=0
		while c not in x:
			c=getch()
			
		return c-48
	else:
		return 0
		
def roll_dice(n,f,x):
#roll NdF+X
	score=0
	for i in range(n):
		score+=random.randint(1,f)
	
	score+=x
	if score>=0:
		return score
	else:
		return 0
		
def get_m_type():
#return a melee weapon type
	tchance=random.randint(1,100)
	
	if tchance >=95:
		type=POLEARM
	elif tchance >=85 and tchance <95:
		type=STAFF
	elif tchance >=70 and tchance <85:
		type=AXE
	elif tchance >=50 and tchance <70:
		type=MACE
	elif tchance >=25 and tchance <50:
		type=SWORD
	elif tchance >=1 and tchance <25:
		type=DAGGER
		
	return type
	
def get_quality(dlvl):
#according to dungeon level, generate appropriate armor or weapon quality
	q_chance=random.randint(1,100)
	q=0
	if dlvl>45:
		if q_chance>95:
			q=5
		else:
			q=4
	elif dlvl>40 and dlvl <=45:
		if q_chance>98:
			q=5
		elif q_chance>65:
			q=4
		else:
			q=3
	elif dlvl>30 and dlvl <=40:
		if q_chance>98:
			q=4
		elif q_chance>65:
			q=2
		else:
			q=3
	elif dlvl>20 and dlvl <=30:
		if q_chance>98:
			q=3
		elif q_chance>65:
			q=1
		else:
			q=2
	elif dlvl>10 and dlvl <=20:
		if q_chance>98:
			q=2
		elif q_chance>65:
			q=0
		else:
			q=1
	elif dlvl>0 and dlvl <=10:
		if q_chance>90:
			q=1
		else:
			q=0
		
	return q
	
def get_mod(s):
#get the modifier value of a given stat
	return int(math.floor(s/2)-5)